﻿using DBOptionPro.DAL;
using DBOptionPro.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DBOptionPro.BLL
{
    public class MarkBLL
    {
        /// <summary>
        /// 构造
        /// </summary>
        public MarkBLL()
        {
            var option = new SysOptionBLL().GetOption();
            _MarkDAL = new MarkDAL(option);
            //RefreshMarks();
        }

        /// <summary>
        /// 标记数据库访问对象(mysql)
        /// </summary>
        private readonly MarkDAL _MarkDAL;

        /// <summary>
        /// 标记缓存
        /// </summary>
        private static List<MarkInfo> _Marks;

        /// <summary>
        /// 刷新标记缓存
        /// </summary>
        public async void RefreshMarks()
        {
            _Marks = await _MarkDAL.GetAllMarks();
            if (_Marks == null) _Marks = new List<MarkInfo>();
        }

        /// <summary>
        /// 标记
        /// </summary>
        /// <param name="file">文件信息对象</param>
        /// <returns></returns>
        public bool Mark(DBFileInfo file)
        {
            MyDataDAL markDAL = new MyDataDAL(file);
            var ls = markDAL.GetMarkData();
            foreach (var item in ls)
            {
                var markInfo = _Marks.FirstOrDefault(t => t.code == item.ksh);
                if (markInfo != null)
                {
                    item.xm = markInfo.remark;
                    if (!markDAL.UpdateMark(item)) return false;
                }
            }
            return true;
        }

        /// <summary>
        /// 还原
        /// </summary>
        /// <param name="file">文件信息对象</param>
        /// <returns></returns>
        public bool ReMark(DBFileInfo file)
        {
            MyDataDAL markDAL = new MyDataDAL(file);
            var ls = markDAL.GetMarkData();
            foreach (var item in ls)
            {
                var markInfo = _Marks.FirstOrDefault(t => t.code == item.ksh);
                if (markInfo != null)
                {
                    item.xm = markInfo.name;
                    if (!markDAL.UpdateMark(item)) return false;
                }
            }
            return true;
        }
    }
}
